/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control.admin;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Product;
import persistence.PersistenceFactory;
import persistence.PersistenceInterface;

/**
 *
 * @author isen
 */
public class ReadDetailsProductServlet extends CoolServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PersistenceInterface persistenceManager = PersistenceFactory.getInstance(persistenceMechanism);
        String name = request.getParameter("name");
        Product product = persistenceManager.readDetailsProduct(name);
        
        if (product != null) {
            request.setAttribute("productdetails", product);
            gotoURL(informacionesDetalladasProducts, request, response);
            
        } else {
            request.setAttribute("resultado", "Sorry, but the product you are looking for doesn't exist");
            request.getRequestDispatcher("forms/ReadDetailsForm.jsp").forward(request, response);
        }
    }
}
